import Mock from 'mockjs'
// 除了分页以外的数据
var data = Mock.mock('/infor','get',{
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    'list|1-10': [{
        // 属性 id 是一个自增数，起始值为 1，每次增 1
        'id|+1': 1,
        'name':'@cname',
        'image':'@image(300x100,#343434,text)'//336*280
    }],
    'indexList|4':[{
        'id|+1': 1,
        'image':'@image(300x200,@color,@ctitle )'
    }],
    'aboutus':{
        'title':'@cparagraph()'
    },
    'product':{
        'tabs|3':[{
            'title':'@ctitle'
        }],
        'tabMain|3':[{
            'imgurl':'@image(400x250,@color,@ctitle)',
            'content':'@cparagraph()'
        }],
        'cur':0
    }
})
// 输出结果
console.log(JSON.stringify(data, null, 4))

module.export=()=>{
    return data ,bookData
}



var bookData = Mock.mock({
    "booklist|50": [
        {
            "row_index|+1": 1, 
            "book_id|+1": 101, 
            "book_title":"@ctitle",
            "book_name": "@cparagraph()",
            "book_time":"@time('yyyy-MM-dd')"
        }
    ],
    "listsize": 50
});

Mock.mock(new RegExp("/list.*"), "get", (options)=> {
    const page = GET("page", options.url, 1), pagesize = GET("pagesize", options.url, 5);
    return getDataByPage(page, pagesize, bookData, "booklist")
});
function getDataByPage(page, pagesize, data, key) { //根据页码 页尺寸 来过滤数据
 
    const start = (page - 1) * pagesize;
    const end = page * pagesize > data[key].length ? data[key].length : page * pagesize;
    let result = [];
    for (let i = start; i < end; i++) {
        result.push(data[key][i]);// [{book_id},{}]
    }
    const ret = {};
    ret[key] = result;
    ret["listsize"] = data.listsize;
    return ret;
}
// /**
//  * @return {string}
//  */
function GET(paramName, url, defValue) {
    const arrObj = url.split("?");
    if (arrObj.length > 1) {
        const arrPara = arrObj[1].split("&");
        let arr;
        for (let i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");
            if (arr !== null && arr[0] === paramName) {
                return arr[1];
            }
        }
    }
    return defValue;
}
